import sys

private_hub=sys.argv[1]
private_hub_auth=sys.argv[2]

mirrors_config="""        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."{private_hub}"]
          endpoint = ["http://{private_hub}"]
      [plugins."io.containerd.grpc.v1.cri".registry.configs]
        [plugins."io.containerd.grpc.v1.cri".registry.configs."{private_hub}".tls]
          insecure_skip_verify = true
        [plugins."io.containerd.grpc.v1.cri".registry.configs."{private_hub}".auth]
          username = ""
          password = ""
          auth = "{private_hub_auth}"
          identitytoken = ""
""".format(private_hub=private_hub, private_hub_auth=private_hub_auth)

searchline = 'https://registry-1.docker.io'
s=''

with open('/etc/containerd/config.toml', 'r') as f:
    lines = f.readlines()
    for i, line in enumerate(lines):
        if searchline in line:
            lines.insert(i+1, mirrors_config)
            s = ''.join(lines)
            break

with open('/etc/containerd/config.toml', 'w') as f:
    f.write(s)
